package pers.luofei.http.client.services;

import com.alibaba.fastjson.JSON;
import pers.luofei.http.client.core.SimpleHttpProcessor;

import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by luofei on 2017/8/31.
 */
@SuppressWarnings("unchecked")
public class Services {

    private static Map<Class<?>, Object> services = new HashMap<>();

    private static SimpleHttpProcessor simpleHttpProcessor = new SimpleHttpProcessor();

    public static SimpleService get() {

        return newInstance(SimpleService.class);
    }

    public static <T> T get(Class<T> clazz) {

        Object service = services.get(clazz);
        if (service == null) {
            service = newInstance(clazz);
        }
        return (T) service;
    }

    private static <T> T newInstance(Class<T> clazz) {

        return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, simpleHttpProcessor);
    }

    public static Object get0(String url) {

        return get(SimpleService.class).get0(url);
    }

    public static Object get0(String url, Map<String, String> headers) {

        return get(SimpleService.class).get0(url, headers);
    }

    public static JSON get(String url) {

        return get(SimpleService.class).get(url);
    }

    public static JSON get(String url, Map<String, String> headers) {

        return get(SimpleService.class).get(url, headers);
    }

    public static Object post0(String url) {

        return get(SimpleService.class).post0(url);
    }

    public static JSON post(String url) {

        return get(SimpleService.class).post(url);
    }

    public static Object post0(String url, Map<String, String> params) {

        return get(SimpleService.class).post0(url, params);
    }

    public static Object post0(String url, Object body) {

        return get(SimpleService.class).post0(url, body);
    }

    public static Object post0(String url, Map<String, String> params, Map<String, String> headers) {

        return get(SimpleService.class).post0(url, params, headers);
    }

    public static Object post0(String url, Object body, Map<String, String> headers) {

        return get(SimpleService.class).post0(url, body, headers);
    }

    public static JSON post(String url, Map<String, String> params) {

        return get(SimpleService.class).post(url, params);
    }

    public static JSON post(String url, Object body) {

        return get(SimpleService.class).post(url, body);
    }

    public static JSON post(String url, Map<String, String> params, Map<String, String> headers) {

        return get(SimpleService.class).post(url, params, headers);
    }

    public static JSON post(String url, Object body, Map<String, String> headers) {

        return get(SimpleService.class).post(url, body, headers);
    }

    public static Object put0(String url) {

        return get(SimpleService.class).put0(url);
    }

    public static JSON put(String url) {

        return get(SimpleService.class).put(url);
    }

    public static Object put0(String url, Map<String, String> params) {

        return get(SimpleService.class).put0(url, params);
    }

    public static Object put0(String url, Object body) {

        return get(SimpleService.class).put0(url, body);
    }

    public static Object put0(String url, Map<String, String> params, Map<String, String> headers) {

        return get(SimpleService.class).put0(url, params, headers);
    }

    public static Object put0(String url, Object body, Map<String, String> headers) {

        return get(SimpleService.class).put0(url, body, headers);
    }


    public static JSON put(String url, Map<String, String> params) {

        return get(SimpleService.class).put(url, params);
    }

    public static JSON put(String url, Object body) {

        return get(SimpleService.class).put(url, body);
    }

    public static JSON put(String url, Map<String, String> params, Map<String, String> headers) {

        return get(SimpleService.class).put(url, params, headers);
    }

    public static JSON put(String url, Object body, Map<String, String> headers) {

        return get(SimpleService.class).put(url, body, headers);
    }

    public static Object delete0(String url) {

        return get(SimpleService.class).delete0(url);
    }

    public static Object delete0(String url, Map<String, String> headers) {

        return get(SimpleService.class).delete0(url, headers);
    }

    public static JSON delete(String url) {

        return get(SimpleService.class).delete(url);
    }

    public static JSON delete(String url, Map<String, String> headers) {

        return get(SimpleService.class).delete(url, headers);
    }
}